//
//  ProjectBean.swift
//  test-ios
//
//  Created by wyz5204 on 2023/7/1.
//

import Foundation
// MARK: - ProjectBean
struct ProjectBean: Codable {
    let id: Int
    let code, currentImplementStageName: String
    let currentImplementStageID: Int
    let crmID: JSONNull?
    let name: String
    let abbreviation: String?
    let financingTopID, currentStageID, financingID: Int
    let financingName: String
    let deptID: Int
    let createTime: String
    let createBy: Int
    let updateBy, updateTime: JSONNull?
    let completeFlag: Int
    let delFlag: JSONNull?
    let endFlag: Int
    let endTime, startTime, topTime: JSONNull?
    let deptName, deptPath: String
    let linkProjectID, linkProjectName, useTplID, commonlyStatus: JSONNull?
    let projectNameNote: String?
    let top: Bool

    enum CodingKeys: String, CodingKey {
        case id, code, currentImplementStageName
        case currentImplementStageID = "currentImplementStageId"
        case crmID = "crmId"
        case name, abbreviation
        case financingTopID = "financingTopId"
        case currentStageID = "currentStageId"
        case financingID = "financingId"
        case financingName
        case deptID = "deptId"
        case createTime, createBy, updateBy, updateTime, completeFlag, delFlag, endFlag, endTime, startTime, topTime, deptName, deptPath
        case linkProjectID = "linkProjectId"
        case linkProjectName
        case useTplID = "useTplId"
        case commonlyStatus, projectNameNote, top
    }
}

// MARK: ProjectBean convenience initializers and mutators

extension ProjectBean {
    init(data: Data) throws {
        self = try newJSONDecoder().decode(ProjectBean.self, from: data)
    }

    init(_ json: String, using encoding: String.Encoding = .utf8) throws {
        guard let data = json.data(using: encoding) else {
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
        }
        try self.init(data: data)
    }

    init(fromURL url: URL) throws {
        try self.init(data: try Data(contentsOf: url))
    }

    func with(
        id: Int? = nil,
        code: String? = nil,
        currentImplementStageName: String? = nil,
        currentImplementStageID: Int? = nil,
        crmID: JSONNull?? = nil,
        name: String? = nil,
        abbreviation: String? = nil,
        financingTopID: Int? = nil,
        currentStageID: Int? = nil,
        financingID: Int? = nil,
        financingName: String? = nil,
        deptID: Int? = nil,
        createTime: String? = nil,
        createBy: Int? = nil,
        updateBy: JSONNull?? = nil,
        updateTime: JSONNull?? = nil,
        completeFlag: Int? = nil,
        delFlag: JSONNull?? = nil,
        endFlag: Int? = nil,
        endTime: JSONNull?? = nil,
        startTime: JSONNull?? = nil,
        topTime: JSONNull?? = nil,
        deptName: String? = nil,
        deptPath: String? = nil,
        linkProjectID: JSONNull?? = nil,
        linkProjectName: JSONNull?? = nil,
        useTplID: JSONNull?? = nil,
        commonlyStatus: JSONNull?? = nil,
        projectNameNote: String? = nil,
        top: Bool? = nil
    ) -> ProjectBean {
        return ProjectBean(
            id: id ?? self.id,
            code: code ?? self.code,
            currentImplementStageName: currentImplementStageName ?? self.currentImplementStageName,
            currentImplementStageID: currentImplementStageID ?? self.currentImplementStageID,
            crmID: crmID ?? self.crmID,
            name: name ?? self.name,
            abbreviation: abbreviation ?? self.abbreviation,
            financingTopID: financingTopID ?? self.financingTopID,
            currentStageID: currentStageID ?? self.currentStageID,
            financingID: financingID ?? self.financingID,
            financingName: financingName ?? self.financingName,
            deptID: deptID ?? self.deptID,
            createTime: createTime ?? self.createTime,
            createBy: createBy ?? self.createBy,
            updateBy: updateBy ?? self.updateBy,
            updateTime: updateTime ?? self.updateTime,
            completeFlag: completeFlag ?? self.completeFlag,
            delFlag: delFlag ?? self.delFlag,
            endFlag: endFlag ?? self.endFlag,
            endTime: endTime ?? self.endTime,
            startTime: startTime ?? self.startTime,
            topTime: topTime ?? self.topTime,
            deptName: deptName ?? self.deptName,
            deptPath: deptPath ?? self.deptPath,
            linkProjectID: linkProjectID ?? self.linkProjectID,
            linkProjectName: linkProjectName ?? self.linkProjectName,
            useTplID: useTplID ?? self.useTplID,
            commonlyStatus: commonlyStatus ?? self.commonlyStatus,
            projectNameNote: projectNameNote ?? self.projectNameNote,
            top: top ?? self.top
        )
    }

    func jsonData() throws -> Data {
        return try newJSONEncoder().encode(self)
    }

    func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
        return String(data: try self.jsonData(), encoding: encoding)
    }
}
